package com.swak.boot.config.redis;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;

import com.swak.annotation.redis.RedisSubscriber;
import com.swak.boot.booter.ApplicationBooter;
import com.swak.utils.Sets;

/**
 * 消费者启动
 * 
 * @author lifeng
 */
public class RedisMqPostProcessor implements BeanPostProcessor, ApplicationBooter {

	private final Set<Object> subscribeBeans = Sets.newHashSet();

	/**
	 * 初始化结束之后注册 -- 只需要找到一个即可
	 */
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		Class<?> clazz = bean.getClass();
		Method[] methods = clazz.getMethods();
		for (Method method : methods) {
			if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) {
				RedisSubscriber reference = method.getAnnotation(RedisSubscriber.class);
				if (reference != null) {
					subscribeBeans.add(bean);
					break;
				}
			}
		}
		return bean;
	}

	/**
	 * 注意： <br>
	 * 1. 如果是服务器环境则需要等待服务器启动之后再初始化其他组件<br>
	 * 2. 如果是应用环境则可以直接启动<br>
	 * 
	 * @param context
	 */
	@Override
	public void onApplicationEvent(ApplicationContext context) {
		
	}
}